#include "graphics.h"
#include "mathutil.h"

static HWND gHwnd;


//draw filled rectangle
void FillRect(int x, int y, int sx, int sy, unsigned int color){

   HDC hdc=GetDC(gHwnd);
   HBRUSH br = CreateSolidBrush(color);
   RECT rc;
   SetRect(&rc, x, y, x+sx, y+sy);
   FillRect(hdc, &rc, br);
   DeleteObject(br);
   ReleaseDC(gHwnd, hdc);
}


//draw line
void DrawLine(int x1, int y1, int x2, int y2, unsigned int color){

   HDC hdc=GetDC(gHwnd);
   HPEN pen = CreatePen(PS_SOLID, 1, color);
   HGDIOBJ old_pen = SelectObject(hdc, pen);

   MoveToEx(hdc, x1, y1, NULL);
   LineTo(hdc, x2, y2);

   SelectObject(hdc, old_pen);
   DeleteObject(pen);
   ReleaseDC(gHwnd, hdc);
}

//draw line
void DrawLine(vec3 a, vec3 b, unsigned int color)
{
    DrawLine(round(a.X), round(a.Y), round(b.X), round(b.Y), color);
}

//initialize "library"
HWND InitializeGraphics(const s_init& init){


   WNDCLASSEX wc;
   memset(&wc, 0, sizeof(wc));
   wc.cbSize = sizeof(WNDCLASSEX);
   wc.lpfnWndProc = init.wndProc;
   wc.hInstance = init.instance;
   wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
   wc.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
	wc.lpszClassName = "MainWindow";
   RegisterClassEx(&wc);

   DWORD dw_style = WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_CLIPCHILDREN;
   RECT rc;
   SetRect(&rc, 0, 0, init.screenWidth, init.screenHeight);
   AdjustWindowRect(&rc, dw_style, false);
   rc.right -= rc.left;
   rc.bottom -= rc.top;

   gHwnd = CreateWindowEx(0,
      "MainWindow",           //ClassName
      init.captionText,
      dw_style | WS_VISIBLE,
      (GetSystemMetrics(SM_CXSCREEN)-rc.right)/2,
      (GetSystemMetrics(SM_CYSCREEN)-rc.bottom)/2,
      rc.right, rc.bottom,
      NULL,                   //hwndParent
      NULL,
      init.instance,
      NULL);                  //lpvParam

	return gHwnd;
}


//Unitialize graphic "library"
void UnitializeGraphics(){

	DestroyWindow(gHwnd);
}
